package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Adapter.AnimRFRecyclerViewAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.RecentBattle;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.R;

public class AllHisttoryActivity extends AppCompatActivity {
    private XRecyclerView animRFRecyclerView;
    private AnimRFRecyclerViewAdapter adapter;
    private RecentBattle recentBattle;
    private int page = 0;
    private List<RecentBattle.DataBean.DataBean2.BattleListBean> mlist=new ArrayList<>();
    private List<RecentBattle.DataBean.DataBean2.BattleListBean> templist=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_histtory);
        initView();
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpUtils.doPostAsyn("http://lol.magnet.zhangyoubao.com/service/rest?api=match.lastlist&game=lol&platformVersion=400901&modle_=MX4",
                            "apiVersion=v1&platform=android&os=android&osVersion=17&secretSignature=N%2FZ1MrDsGxj5jM4q9oJRjVu2D0Q%3D&game=lol&params%5Brequest%5D=%7B%22uid%22%3A%22%7B%5C%22tgp%5C%22%3A%5C%22U18084499025935679780%5C%22%7D%22%2C%22area%22%3A%226%22%2C%22page%22%3A" + page + "%2C%22name%22%3A%22%E5%B3%B0%E5%93%A5%E6%97%A0%E6%95%8C%E5%A6%B9%E5%AD%90%E6%9D%A5%E8%A2%AD%22%2C%22b_type%22%3A%22%E5%85%A8%E9%83%A8%22%2C%22is_current%22%3A1%7D&sign=&platformVersion=400902&time=1474286050&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&nonce=477036&userToken=1285c08b612411d0bfc35f213eb5d92a73&secretVersion=v1.0&params%5Bver%5D=27&userId=15863305&api=match.list&deviceId=Lf2he%2FrdpOPtR1PwqdUiuwS64wm6xMV8rBpebf4NEuI%3D&",
                            new HttpUtils.CallBack() {
                                @Override
                                public void onRequestComplete(String result) {
                                    Gson gson = new Gson();
                                    recentBattle = gson.fromJson(result, RecentBattle.class);
                                    templist = recentBattle.getData().getData().getBattle_list();
                                }
                            }, handler);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        animRFRecyclerView = (XRecyclerView) findViewById(R.id.animRFRecyclerView);
        //上拉加载的动画效果
        animRFRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        animRFRecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        animRFRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 0;
                initData();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                initData();
            }
        });
        findViewById(R.id.edit_head_left_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (page == 0) {
                mlist.clear();
                mlist.addAll(templist);
                adapter = new AnimRFRecyclerViewAdapter(AllHisttoryActivity.this, mlist);
                animRFRecyclerView.setAdapter(adapter);
                animRFRecyclerView.setLayoutManager(new LinearLayoutManager(AllHisttoryActivity.this));
                animRFRecyclerView.refreshComplete();
                templist.clear();
            } else {
                mlist.addAll(templist);
                adapter.notifyDataSetChanged();
                animRFRecyclerView.loadMoreComplete();
                templist.clear();
            }

        }
    };
}
